<!--
 * @Description: 编辑服务
 * @Author: xiao li
 * @Date: 2021-07-03 11:41:05
 * @LastEditTime: 2022-09-15 16:23:20
 * @LastEditors: xiao li
-->

<template>
  <div class="lb-system-banner-edit">
    <top-nav :title="navTitle" :isBack="true" />
    <div class="page-main">
      <el-form
        @submit.native.prevent
        :model="subForm"
        ref="subForm"
        :rules="subFormRules"
        label-width="130px"
      >
        <el-form-item label="关联用户" prop="user_id">
          <el-tag @click="toShowDialog">{{
            subForm.user_id ? subForm.nickName : '选择关联用户'
          }}</el-tag>
        </el-form-item>
        <el-form-item label="技师姓名" prop="coach_name">
          <el-input
            v-model="subForm.coach_name"
            maxlength="15"
            show-word-limit
            placeholder="请输入技师姓名"
          ></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="subForm.sex">
            <el-radio :label="0">男</el-radio>
            <el-radio :label="1">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input
            v-model="subForm.mobile"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <el-form-item label="从业年份" prop="work_time">
          <el-input
            v-model.number="subForm.work_time"
            placeholder="请输入从业年份"
          ></el-input>
        </el-form-item>
        <el-form-item label="意向工作城市" prop="city_id">
          <!-- <el-input
            v-model="subForm.city"
            placeholder="请输入意向工作城市"
          ></el-input> -->
          <el-select v-model="subForm.city_id" placeholder="请选择">
            <el-option
              v-for="item in cityList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所在地址" prop="address">
          <el-input
            v-model="subForm.address"
            placeholder="请输入所在地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="经度" prop="lng">
          <el-input v-model="subForm.lng" placeholder="请输入经度"></el-input>
        </el-form-item>
        <el-form-item label="纬度" prop="lat">
          <el-input v-model="subForm.lat" placeholder="请输入纬度"></el-input>
          <lb-button @click="showMap = true" type="primary" plain size="mini"
            >获取经纬度</lb-button
          >
        </el-form-item>
        <el-form-item label="技师简介" prop="text">
          <el-input
            type="textarea"
            :rows="10"
            maxlength="300"
            resize="none"
            show-word-limit
            placeholder="请输入技师简介"
            v-model="subForm.text"
          ></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="id_code">
          <el-input
            v-model="subForm.id_code"
            placeholder="请输入身份证号"
          ></el-input>
        </el-form-item>
        <el-form-item label="身份证照片" prop="id_card">
          <lb-cover
            :fileList="subForm.id_card"
            fileType="image"
            type="more"
            @selectedFiles="getBannerList($event, 'id_card')"
            :fileSize="3"
          ></lb-cover>
          <lb-tool-tips
            >请分别上传身份证人像面、身份证国徽面、手持身份证照片</lb-tool-tips
          >
        </el-form-item>
        <el-form-item label="资格证书" prop="license">
          <lb-cover
            :fileList="subForm.license"
            fileType="image"
            type="more"
            @selectedFiles="getBannerList($event, 'license')"
            :fileSize="15"
          ></lb-cover>
        </el-form-item>
        <el-form-item label="工作形象照" prop="work_img">
          <lb-cover
            :fileList="subForm.work_img"
            tips="400 * 400"
            @selectedFiles="getCover($event, 'work_img')"
          ></lb-cover>
        </el-form-item>
        <el-form-item label="个人生活照" prop="self_img">
          <lb-cover
            :fileList="subForm.self_img"
            fileType="image"
            type="more"
            tips="750 * n"
            @selectedFiles="getBannerList($event, 'self_img')"
            :fileSize="9"
          ></lb-cover>
        </el-form-item>

        <el-form-item label="个人视频介绍" prop="video">
          <div class="upload-file-warp">
            <input
              type="text"
              class="choice-file-input"
              v-model="subForm.video"
              placeholder="请选择视频"
            />
            <lb-cover
              type="button"
              fileType="video"
              :fileSize="1"
              @selectedFiles="getVoice"
            ></lb-cover>
          </div>
        </el-form-item>
        <el-form-item>
          <lb-button type="primary" @click="submitForm">{{
            $t('action.submit')
          }}</lb-button>
          <lb-button @click="$router.back(-1)">{{
            $t('action.back')
          }}</lb-button>
        </el-form-item>
      </el-form>

      <el-dialog
        title="关联用户"
        :visible.sync="showDialog"
        width="800px"
        center
      >
        <el-form
          :inline="true"
          :model="searchForm"
          ref="searchForm"
          label-width="70px"
        >
          <el-form-item label="输入查询" prop="nickName">
            <el-input
              v-model="searchForm.nickName"
              placeholder="请输入用户昵称/手机号"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <lb-button
              size="medium"
              type="primary"
              icon="el-icon-search"
              style="margin-right: 5px"
              @click="getTableDataList(1)"
              >{{ $t('action.search') }}</lb-button
            >
            <lb-button
              size="medium"
              icon="el-icon-refresh-left"
              style="margin-right: 5px"
              @click="resetForm('searchForm')"
              >{{ $t('action.reset') }}</lb-button
            >
          </el-form-item>
        </el-form>
        <el-table
          :data="tableData"
          ref="singleTable"
          :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
          tooltip-effect="dark"
          style="width: 100%"
          highlight-current-row
          @current-change="handleSelectionChange"
        >
          <el-table-column prop="id" label="用户ID"></el-table-column>
          <el-table-column prop="avatarUrl" label="头像">
            <template slot-scope="scope">
              <lb-image :src="scope.row.avatarUrl" />
            </template>
          </el-table-column>
          <el-table-column prop="nickName" label="昵称"></el-table-column>
          <el-table-column prop="phone" label="手机号"></el-table-column>
        </el-table>
        <lb-page
          :batch="false"
          :page="searchForm.page"
          :pageSize="searchForm.limit"
          :total="total"
          @handleSizeChange="handleSizeChange"
          @handleCurrentChange="handleCurrentChange"
        >
        </lb-page>
        <span slot="footer" class="dialog-footer">
          <el-button @click="showDialog = false">取 消</el-button>
          <el-button type="primary" @click="handleDialogConfirm"
            >确 定</el-button
          >
        </span>
      </el-dialog>
      <lb-map
        :dialogVisible.sync="showMap"
        @selectedLatLng="getLatLng"
      ></lb-map>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      cityList: [],
      id: '',
      navTitle: '',
      showMap: false,
      subForm: {
        id: 0,
        user_id: 0,
        nickName: '',
        coach_name: '', // 姓名
        mobile: '', // 手机号
        sex: 0, // 性别
        work_time: '', // 从业年份
        city_id: '', // 城市id
        lng: '',
        lat: '',
        address: '', // 所在地址
        text: '', // 备注
        id_code: '', // 身份证号
        id_card: [], // 身份证
        license: [], // 资格证书
        work_img: [], // 工作照
        self_img: [], // 生活照
        video: '',
        status: 2
      },
      subFormRules: {
        coach_name: {
          required: true,
          type: 'string',
          message: '请输入技师姓名',
          trigger: 'blur'
        },
        mobile: {
          required: true,
          validator: this.$reg.valiDateTel,
          trigger: 'blur'
        },
        sex: {
          required: true,
          type: 'number',
          message: '请选择',
          trigger: 'blur'
        },
        work_time: {
          required: true,
          type: 'number',
          message: '请输入从业年份',
          trigger: 'blur'
        },
        city_id: {
          required: true,
          message: '请选择意向工作城市',
          trigger: 'blur'
        },
        address: {
          required: true,
          type: 'string',
          message: '请输入地址',
          trigger: 'blur'
        },
        lng: {
          required: true,
          message: '请输入经纬度',
          trigger: ['change', 'blur']
        },
        lat: {
          required: true,
          message: '请输入经纬度',
          trigger: ['change', 'blur']
        },
        id_code: {
          required: true,
          validator: this.$reg.isIdCard,
          trigger: 'blur'
        },
        text: {
          required: true,
          type: 'string',
          message: '请输入技师简介',
          trigger: 'blur'
        },
        id_card: {
          required: true,
          type: 'array',
          message: '请上传图片',
          trigger: 'blur'
        },
        license: {
          required: true,
          type: 'array',
          message: '请上传图片',
          trigger: 'blur'
        },
        work_img: {
          required: true,
          type: 'array',
          message: '请上传图片',
          trigger: 'blur'
        },
        self_img: {
          required: true,
          type: 'array',
          message: '请上传图片',
          trigger: 'blur'
        }
      },
      searchForm: {
        page: 1,
        limit: 10,
        nickName: ''
      },
      total: 0,
      loading: false,
      tableData: [],
      currentRow: {},
      showDialog: false
    }
  },
  created () {
    let { id } = this.$route.query
    if (id) {
      this.subForm.id = id
      this.getDetail(id)
    }
    this.navTitle = this.$t(id ? 'menu.TechnicianEdit' : 'menu.TechnicianAdd')
    this.getCityList()
  },
  methods: {
    async getCityList (e) {
      let { code, data } = await this.$api.system.citySelect()
      console.log('data', data)
      if (code !== 200) return
      this.cityList = data
    },
    getCover (img, key) {
      this.subForm[key] = img
    },
    getBannerList (imgs, key) {
      this.subForm[key].push(...imgs)
    },
    /**
     * @method 获取经纬度
     */
    getLatLng (latLng) {
      this.subForm.lat = latLng.lat
      this.subForm.lng = latLng.lng
    },
    /**
     * @name: 详情
     * @param {*} id
     */
    async getDetail (id) {
      let { code, data } = await this.$api.technician.coachInfo({ id })
      if (code !== 200) return
      data.nickName = data.user_id
        ? data.nickName || `用户ID ${data.user_id}`
        : ''
      data.work_img = [{ url: data.work_img }]
      let arr = ['id_card', 'license', 'self_img']
      arr.map((item) => {
        data[item] = data[item].map((aitem) => {
          return { url: aitem }
        })
      })
      for (let key in this.subForm) {
        this.subForm[key] = data[key]
      }
    },
    getVoice (file) {
      let len = file.length - 1
      this.subForm.video = file[len].url
    },
    async toShowDialog () {
      if (this.subForm.id) return
      this.searchForm.nickName = ''
      await this.getTableDataList()
      this.showDialog = !this.showDialog
    },
    resetForm (form) {
      this.$refs[form].resetFields()
      this.getTableDataList(1)
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    async getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      let param = JSON.parse(JSON.stringify(this.searchForm))
      let { code, data } = await this.$api.technician.coachUserList(param)
      this.loading = false
      if (code !== 200) return
      this.tableData = data.data
      this.total = data.total
    },
    handleSelectionChange (val) {
      val = JSON.parse(JSON.stringify(val))
      let { id, nickName } = val
      val.nickName = nickName || `用户ID ${id}`
      this.currentRow = val
    },
    handleDialogConfirm () {
      let { id = 0, nickName = '' } = this.currentRow
      this.subForm.user_id = id
      this.subForm.nickName = nickName
      this.showDialog = false
    },
    /**
     * @name: 新增/编辑
     * @param {*}
     */
    submitForm () {
      let flag = true
      this.$refs['subForm'].validate((valid) => {
        if (!valid) flag = false
      })
      if (flag) {
        let subForm = JSON.parse(JSON.stringify(this.subForm))
        if (subForm.id_card.length < 3) {
          this.$message.error(`身份证照片请分别上传身份证人像面、身份证国徽面、手持身份证照片`)
          return
        }
        let arr = ['id_card', 'license', 'self_img']
        arr.map((item) => {
          subForm[item] = subForm[item].map((aitem) => {
            return aitem.url
          })
        })
        subForm.work_img = subForm.work_img[0].url
        delete subForm.nickName
        this.$api.technician.coachAdd(subForm).then((res) => {
          if (res.code === 200) {
            this.$message.success(
              this.$t(
                subForm.id
                  ? 'tips.successRev'
                  : 'tips.successSub'
              )
            )
            this.$router.back(-1)
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-system-banner-edit {
  width: 100%;
  .el-form {
    width: 100%;
    .el-select,
    .el-input-number,
    .el-input {
      width: 300px;
    }
    .el-textarea {
      width: 600px;
    }
    .el-tag {
      cursor: pointer;
    }
  }
}
</style>
